package com.kingtsoft.study.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.kingtsoft.study.common.ContextHolder;
import com.kingtsoft.study.common.JsonResult;
import com.kingtsoft.study.constant.DescribableEnum;
import com.kingtsoft.study.model.StudyUser;
import com.kingtsoft.study.service.StudyUserService;

@Controller
@RequestMapping("/login")
public class LoginController extends MultiActionController
{
	
	@Autowired
	private StudyUserService studyUserService;
	
    
    @RequestMapping(value = "/gotoMain", method = RequestMethod.GET)
    public ModelAndView gotoMain(HttpServletRequest request) {
    	
        ModelAndView mv = new ModelAndView();
        mv.addObject("username", studyUserService.getLoginName());
        mv.setViewName("main/main");
        return mv;
    }
    
    /**
     * 
     * @Title: doLogin 
     * @Description: 用户登录
     * @author: shenlx
     * @param request
     * @param response
     * @param studyUser
     * @return
     * @return: Object
     */
	 @RequestMapping("/doLogin")
	 @ResponseBody
	 public Object doLogin(HttpServletRequest request, HttpServletResponse response,StudyUser studyUser){
		 
		 studyUserService.doLogin(studyUser);
		 return new JsonResult(DescribableEnum.SUCCESS);
	 }
	 
	 /**
	  * 
	  * @Title: register 
	  * @Description: 打开注册页面
	  * @author: shenlx
	  * @param request
	  * @return
	  * @return: ModelAndView
	  */
     @RequestMapping(value = "/register", method = RequestMethod.GET)
     public ModelAndView register(HttpServletRequest request) {
    	
         ModelAndView mv = new ModelAndView();
         mv.setViewName("login/register");
         return mv;
     } 
     
     /**
      * 
      * @Title: remoteUsername 
      * @Description: 校验用户名唯一性
      * @author: shenlx
      * @param request
      * @param response
      * @param username
      * @return
      * @return: Object
      */
	 @RequestMapping("/remoteUsername")
	 @ResponseBody
	 public Object remoteUsername(HttpServletRequest request, HttpServletResponse response,String username){
		 return studyUserService.remoteUsername(username);
	 }
	 
	 /**
	  * 
	  * @Title: addRegister 
	  * @Description: 用户注册
	  * @author: shenlx
	  * @param request
	  * @param response
	  * @param studyUser
	  * @param studentName  这里为了方便，只做了学生注册，并只保存学生姓名
	  * @return
	  * @return: Object
	  */
	 @RequestMapping("/addRegister")
	 @ResponseBody
	 public Object addRegister(HttpServletRequest request, HttpServletResponse response,StudyUser studyUser,String studentName){
		 
		 studyUserService.addRegister(studyUser, studentName);
		 
		 return new JsonResult(DescribableEnum.SUCCESS);
		 
	 }
	 /**
	  * 
	  * @Title: doLogout 
	  * @Description: 登出
	  * @param request
	  * @return
	  * @return: ModelAndView
	  */
	 
     @RequestMapping(value = "/logout", method = RequestMethod.GET)
     public ModelAndView doLogout(HttpServletRequest request) {
    	 try{
    	 studyUserService.doLogout();
    	 }catch(Exception e){
    		 e.printStackTrace();
    	 }
         return new ModelAndView("redirect:/");
     } 

}